<?php
/*
    Class: Friends_IndexController

    About: Author
    Jaybill McCarthy

    About: License
    <http://communit.as/docs/license>
*/
class Friends_IndexController extends Cts_Controller_Action_Abstract {
    
	function init()
    {
        parent::init();
    }

    function indexAction(){
    	if($this->_auth->hasIdentity()){
			$my_username = $this->_identity->username;
		} else {
			$my_username = null;
		}
    
    	$username = $this->_request->getParam('username',$my_username);    	
    	$friends_index_table = new FriendsFriendsIndex();
    	$users_table = new Users();
    	$ui_table = new UsersIndex();
    	$user_where = $users_table->getAdapter()->quoteInto('username = ?',$username);
    	$user = $users_table->fetchRow($user_where);
    	if(is_null($user)){
    		$this->_redirect("/auth/missing");
    	} else {
    		$this->view->user = $user->toArray();
    	}
    	$where = $friends_index_table->getAdapter()->quoteInto('my_username = ?',$username); 
    	$friends = $friends_index_table->fetchAll($where);
    	$tmp_users = array();
    	if(count($friends) > 0){
    		foreach($friends as $friend){
    			$user_where = $ui_table->getAdapter()->quoteInto('username = ?',$friend->username);
    			$user = $ui_table->fetchRow($user_where);
    			if(!is_null($user)){
    				$user = $user->toArray();
		    		$countries_table = new Countries();
		    		$where = $countries_table->getAdapter()->quoteInto('country_code = ?',$user['country_code']);
		    		$country = $countries_table->fetchRow($where);
		    		if(!is_null($country)){
		    			$user['location'] = $country->country; 
		    		}
		    		$tmp_users[] = $user;
    			}
        	}
    		$this->view->friends = $tmp_users;    		
    	}
    	if($username == $my_username){
    		$this->view->pagetitle = "Your Friend List";
    	} else {
    		$this->view->pagetitle = $user->full_name . "'s Friend List";    		
    	}
    	    	
    }
    
    function inviteAction(){    	
    	$friends_table = new FriendsFriends();
    	$users_table = new Users();   	
    	$username = $this->_request->getParam('username',null);
    	$errors = array();
    	$this->view->showForm = false;
    	    	
    	if($username == $this->_identity->username){
    		$errors[] = "You cannot be your own friend. It's just...creepy.";
    	}
    	
    	
    	$where = $users_table->getAdapter()->quoteInto('username = ?',$username);
    	$user = $users_table->fetchRow($where);
    	
    	if(is_null($user)){
    		$errors[] = "The user you are trying to befriend does not exist.";
    	} else {
    		 $this->view->user = $user->toArray();   		
    	}
    	
    	if($friends_table->isRequestPending($this->_identity->username,$username)){
    		$errors[] = "You have already invited ". $username ." to be your friend. You have not been approved yet.";
    	}
    	
    	if($friends_table->isFriend($this->_identity->username,$username)){
    		$errors[] = "You and ". $username ." are already friends.";
    	}
    	
    	if($friends_table->isRequestPending($username,$this->_identity->username)){
    		$errors[] 
    			= $username . " already sent <i>you</i> a <a href='/friends/index/requests'>friend request</a>.";
    	}
    	

    		
    	if(count($errors) == 0){
    		
 			$this->view->pagetitle = "Add " . $user->full_name . "As A Friend";
 			 			
	    	if($this->_request->isPost()){
	    		$invite = $this->_request->getPost('invite');
	    		if($invite == "Yes"){
		    		
		    		$data = array(
		    			'username' => $this->_identity->username,
		    			'friend_username' => $user->username,
		    		);
		    		$friends_table->insert($data);		    		
		    		// send notification email
		    		// TODO Check to see if this user wishes to be notified about this			
					$subject = "New friend request from " . $this->_identity->full_name;
					$email_params = array(
						"url" => Cts_Registry::get('site_url')."/friends/index/requests/",
						"from"	=> Cts_Registry::get('site_from'),				
					);				
					$email = new Cts_Email(null,"friends");				
					$email->sendEmail($subject,$user->email,"newrequest.tpl",$email_params);					
		    		
	    		}
	    		$this->_redirect('/profile/'.$username);
	    		
	    	} else {
	    		$this->view->showForm = true;
	    	}
    	} else {    		
    		$this->view->errors = $errors;    	
    	}	
    	
    }
    
    function deleteAction(){
    	$users_table = new Users();
    	$friends_table = new FriendsFriends();
    	$reject_username = $this->_request->getParam('username');
    	$this->view->pagetitle = "End friendship with ".$reject_username;	
    
    	if($this->_request->isPost()){
    		$delete = $this->_request->getPost('delete');
    		if($delete == "Yes"){
    			$reject_where = $friends_table->getAdapter()->QuoteInto('username = ? ',$this->_identity->username);
    			$reject_where .= $friends_table->getAdapter()->QuoteInto( ' and friend_username = ?',$reject_username);
    			$friends_table->delete($reject_where);    			
    		}
    		$this->_redirect('/profile/'.$this->_identity->username);
    	} else {
    		
    		$where = $users_table->getAdapter()->quoteInto('username = ?',$reject_username);    		
    		$user = $users_table->fetchRow($where);
    		if(!is_null($user) and $friends_table->isFriend($this->_identity->username,$reject_username)){
    			$this->view->user = $user->toArray();
    			
    			$this->view->showForm = true;
    		} else {
    			// we're trying to delete a friend we don't have.
    			$this->_redirect('/auth/missing');
    		}
    	}
    	
    	
    }
    
    function requestsAction(){
    	$friends_table = new FriendsFriends();
    	$users_table = new Users();
    	$this->view->user = $users_table->fetchByUsername($this->_identity->username)->toArray();

    	if($this->_request->isPost()){
    		$approve_username = $this->_request->getPost('approve',null);
    		$reject_username = $this->_request->getPost('reject',null);
    		if(!is_null($approve_username)){
    			$data = array('is_approved' => 1);
    			$approve_where = $friends_table->getAdapter()->QuoteInto('username = ? ',$this->_identity->username);
    			$approve_where .= $friends_table->getAdapter()->QuoteInto( ' and friend_username = ?',$approve_username);
    			$friends_table->update($data,$approve_where);
    			$this->view->success = $approve_username . " is now your friend.";
    		}
    		if(!is_null($reject_username)){
    			$reject_where = $friends_table->getAdapter()->QuoteInto('username = ? ',$this->_identity->username);
    			$reject_where .= $friends_table->getAdapter()->QuoteInto( ' and friend_username = ?',$reject_username);
    			$friends_table->delete($reject_where);
    			$this->view->success = "Friend request from " . $reject_username . " rejected.";
    		}
    		
    	}
    	
    	$where = $friends_table->getAdapter()->quoteInto('is_approved = 0 and is_pending = 0 and username = ?',$this->_identity->username);
    	$friend_requests = $friends_table->fetchAll($where);
    	$tmp_friend_requests = array();    	
    	if(!is_null($friend_requests)){
   	
    		$this->view->requests = $friend_requests->toArray();
    	} 	
    	
    	    	
    	$this->view->pagetitle = "Friend Requests";
    	
    } 

   	function pendingAction(){
    	$friends_table = new FriendsFriends();
    	$users_table = new Users();    	
		$this->view->user = $users_table->fetchByUsername($this->_identity->username)->toArray();


    	if($this->_request->isPost()){
    		$reject_username = $this->_request->getPost('cancel',null);

    		if(!is_null($reject_username)){
    			$reject_where = $friends_table->getAdapter()->QuoteInto('username = ? ',$this->_identity->username);
    			$reject_where .= $friends_table->getAdapter()->QuoteInto( ' and friend_username = ?',$reject_username);
    			$friends_table->delete($reject_where);
    			$this->view->success = "Friend request to " . $reject_username . " cancelled.";
    		}
    		
    	}
    	
    	$where = $friends_table->getAdapter()->quoteInto('is_approved = 0 and is_pending = 1 and username = ?',$this->_identity->username);
    	$friend_requests = $friends_table->fetchAll($where);
    	$tmp_friend_requests = array();    	
    	if(!is_null($friend_requests)){
    		    	
    		$this->view->pending = $friend_requests->toArray();
    	}    	    	
    	$this->view->pagetitle = "Pending Requests";
    	
    } 
    
    
    

}